package com.study.streamApi;

import com.study.methodcontrucet.Employee;
import com.study.methodcontrucet.EmployeeDataList;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @program: java8
 * @description: Stream API 操作
 * @author: yangyb
 * @create: 2021-11-13 15:59
 **/
public class StreamApiTest {
    /**
     * @Description: 创建Stream 方式一：通过集合
     * @Param:
     * @return:
     */
    @Test
    public void test1() {
        List<Employee> employees = EmployeeDataList.getEmployees();
        //default Stream<E> stream ():返回一个顺序流
        Stream<Employee> stream = employees.stream();
        //default Stream<E> parallelStream ():返回一个并行流
        Stream<Employee> parallelStream = employees.parallelStream();
    }

    /**
     * @Description: 创建Stream 方式二：通过数组
     * @Param:
     * @return:
     */
    @Test
    public void test2() {
        int[] arr = new int[]{1, 2, 3, 4, 5, 6};
        //调用Arrays类的static<T> Stream<T> stream(T [] array):返回一个流
        IntStream stream = Arrays.stream(arr);
        Employee tom = new Employee(1001, "Tom");
        Employee jack = new Employee(1001, "Jack");
        Employee[] employees = {tom, jack};
        Stream<Employee> employeeStream = Arrays.stream(employees);

    }

    /**
     * @Description: 创建Stream 方式三：通过Stream的of()
     * @Param:
     * @return:
     */
    @Test
    public void test3() {
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);
    }

    /**
     * @Description: 创建Stream 方式四：创建无限流
     * @Param:
     * @return:
     */
    @Test
    public void test4() {
        //迭代：public static<T> Stream<T> iterator (final T seed,final UnaryOperator<T> f)
        //遍历前10个偶数
        Stream.iterate(0, t -> t + 2).limit(10).forEach(System.out::println);
        //生成：public static<T> Stream<T> generator(Supplier<T> s)
        Stream.generate(Math::random).limit(10).forEach(System.out::println);
    }
}
